package com.example.test_activity.fragment;

import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;

import com.example.test_activity.R;
import com.example.test_activity.http.HttpUtil;
import com.example.test_activity.utils.GetScore;
import com.example.test_activity.utils.NoDoubleClickListener;
import com.example.test_activity.viewUtil.CoordinatorMenu;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.annotations.NonNull;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Function;
import io.reactivex.schedulers.Schedulers;
import okhttp3.ResponseBody;

/**
 * 查看成绩
 */
public class ScoreFragment extends Fragment {

    private View view;
    GetScore getScore = new GetScore();

    private ListView listView;
    private List<Map<String, String>> list = new ArrayList<Map<String, String>>();
    private SimpleAdapter adapter;

    private TextView title;
    private ImageView head;
    private CoordinatorMenu menu;
    private ImageView refresh;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        this.view = inflater.inflate(R.layout.fragment_score, container, false);
        init();
        title.setText("成绩");

        return view;
    }

    @Override
    public void onStart() {
        super.onStart();

        getScore();

        head.setOnClickListener(new NoDoubleClickListener() {
            @Override
            protected void onNoDoubleClick(View v) {
                if (menu.isOpened()) {
                    menu.closeMenu();
                } else {
                    menu.openMenu();
                }
            }
        });

        refresh.setOnClickListener(new NoDoubleClickListener() {
            @Override
            protected void onNoDoubleClick(View v) {
                getScore();
            }
        });
    }

    private void init() {
        head = (ImageView) view.findViewById(R.id.iv_head);
        title = (TextView) view.findViewById(R.id.main_title);
        menu = (CoordinatorMenu) getActivity().findViewById(R.id.menu);
        refresh = (ImageView) view.findViewById(R.id.top_refresh);

        listView = (ListView) view.findViewById(R.id.list);
        adapter = new SimpleAdapter(getContext(), list, R.layout.score_item,
                new String[]{"name", "credit", "score", "reason"},
                new int[]{R.id.tv_name, R.id.tv_credit, R.id.tv_score, R.id.tv_reason});
        listView.setAdapter(adapter);
    }

    private void getScore() {

        list.clear();

        HttpUtil.getInstance().sendGetPost().getScore()
                .map(new Function<ResponseBody, List<Map<String, String>>>() {
                    @Override
                    public List<Map<String, String>> apply(@NonNull ResponseBody responseBody) throws Exception {
                        String html = responseBody.string();
                        List<Map<String, String>> temp = new ArrayList<Map<String, String>>();
                        temp.clear();
                        temp = getScore.getScore(html);

                        return temp;
                    }
                })
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<List<Map<String, String>>>() {
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {

                    }

                    @Override
                    public void onNext(@NonNull List<Map<String, String>> temp) {
                        if (!temp.isEmpty())
                        {
                            list.addAll(temp);
                        }
                    }

                    @Override
                    public void onError(@NonNull Throwable e) {

                    }

                    @Override
                    public void onComplete() {
                        adapter.notifyDataSetChanged();
                    }
                });
    }
}
